import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';

//使用vue框架
Vue.use(Vuex);

export default {
    state: {
        hotList: [],
        total: 0,
    },
    mutations: { //给定义的state里的属性设置值
        setgethotList(state, val) {
            //hotList是数组所以需要拼接，concat
            state.hotList = state.hotList.concat(val);
        }
    },
    actions: { //这里定义方法
        gethotList({ commit, state }, pn) {
            let start = (pn - 1) * 3;
            let end = start + 3;

            axios({
                method: 'get',
                url: '/data/news_data.json?pn=1',
            }).then((res) => {
                console.log(res.data);
                let data = res.data;
                if (data.code === 200) {
                    //给hotList赋值
                    commit('setgethotList', data.hotList.slice(start, end));

                    //记录hotlist的总数
                    state.total = data.hotList.length;
                }

            }).catch((error) => {
                console.log(error)
            });
        }

    }

}